<?
# $Id: DBAccessModel.class.php,v 1.4 2007/05/03 09:31:42 altair Exp $
require_once('BaseModel.class.php');
class DBAccess extends BaseModel
{
    public $smsc;
    function save_messages($detail){
        return $this->insert('inbox', $detail);
    }

    function log_debug($data,$message=''){
        echo $message;
        var_dump($data);
        echo "\n\r";
    }

    function get_tmpl($type){
        return $this->return_one("SELECT template from templates WHERE name LIKE '$type' AND activate=1");
    }

    function get_pattern($pattern){
        $pattern = trim($pattern);
        return $this->return_one("SELECT name from patterns WHERE format  LIKE '$pattern'");
    }

    # this just remove the country code
    function num_format($msisdn){
        if (preg_match("/^(\+63|63|0)([0-9]{1,12})/" , trim($msisdn), $matches))
            return "0".$matches[2];
        else
            return $msisdn;
    }

	function validate_msisdn($msisdn){
		$msisdn = $this->txt2num($msisdn);
        if (preg_match("/^(\+63|63|0)([0-9]{1,12})/" ,$msisdn, $matches)) return "0".$matches[2];
		return false;
	}


    # convert text to its numeric equivalent and remove other character returning just numeric value
    function txt2num($i){
        $pattern = array('/i/i','/v/i','/l/i','/o/i','/[^\d]/');
        $replace = array(1,5,1,0,'');
        return preg_replace($pattern, $replace, $i);
    }
    # convert numeric value to its equivalent aphacharacter (zero to letter O and number one to letter L)
    function num2txt($i){
        $pattern = array('/1/i','/0/i');
        $replace = array('l','o');
        return preg_replace($pattern, $replace, $i);
    }

    function send_sms($msisdn, $body, $smsc='',$username='') {
        $smsc = ($smsc) ? $smsc : $this->smsc;
        $msisdn = $this->num_format($msisdn);
        $body = trim($body);
        if ($msisdn && $body) {
            $d['username'] = ($username) ? $username : 'system';
            $d['msisdn']   = $msisdn;
            $d['body']     = $body;
            $d['smsc']     = $smsc;
            $id = $this->insert('outbox', $d);

            $dlr = DLR_URL . '&outbox_id=' . $id;
            $smsc = ""; //DISABLE SMSC USE RAMDOM SENDING;
            $url = SEND_URL . '&to=' . $msisdn . '&text=' . urlencode($body) . '&smsc=' . $smsc . '&dlr-url=' . urlencode($dlr);

            $ch = curl_init ($url);
            ob_start();
            curl_exec($ch);
            $str = ob_get_contents();
            ob_end_clean();
            curl_close ($ch);
            $this->query("update outbox set extra = '$str' where id='$id'");

        }
    }

    function group_members($group){
        $sql = "SELECT m.msisdn
                FROM `groups` g
                JOIN group_members gm ON g.id = gm.group_id
                JOIN members m ON m.id = gm.member_id WHERE g.name='$group'";
        return $this->select_all($sql);
    }

    function get_outstanding($enrollment_id){
        $this->set_db('mcs');
        $sql = "SELECT SUM(outstanding) FROM `assessment_details` WHERE `enrollment_id` = '$enrollment_id'";
        $res = $this->return_one($sql);
        $this->set_db('sms');
        return $res;
    }
    /*function balance($id){
        $this->set_db('mcs');
        $sql = "SELECT SUM(balance_loan)as bal_loan,SUM(balance_home) as bal_home FROM `members` WHERE `id` = '$id'";
        $res = $this->return_one($sql);
        $this->set_db('sms');
        return $res;
    }*/

    function enrollment_id_from_studno($studno){
        $this->set_db('mcs');
        $sql = "SELECT e.id FROM `students` s JOIN enrollments e on e.student_id=s.id
                where s.stud_no = '$studno' order by e.id desc limit 1";
        $res = $this->return_one($sql);
        $this->set_db('sms');
        return $res;
    }
}
?>
